package com.mo2323.merchant.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.mo2323.common.VO;
import com.mo2323.common.VOList;
import com.mo2323.common.util.AjaxObject;
import com.mo2323.merchant.beans.VenueMemberSyncData;
import com.mo2323.service.merchant.IErpService;
import com.mo2323.service.merchant.IFieldReserveRule;
import com.mo2323.service.merchant.ISyncService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author 28876
 */
@Controller
@RequestMapping("/erp")
public class ErpController extends BaseController {
    @Reference(timeout = 5000)
    private IErpService erpService;

    @Reference(timeout = 300 * 1000)
    private ISyncService syncService;

    @Reference(timeout = 5000)
    private IFieldReserveRule fieldReserveRule;

    @RequestMapping(value = "/exchangeableField", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getExchangeableField(@RequestParam String frId) {
        try {
            //DONE:通过slipNumber和获取当前时间可更换的场地
            List<VO> result = erpService.getExchangeableFieldList(frId);
            return AjaxObject.ok().data(VOList.I(result).getData(true));

        } catch (Exception e) {
            logger.error("getExchangeableField Exception：", e);
            return AjaxObject.error("getExchangeableField Exception：" + e.getMessage());
        }
    }

    @RequestMapping(value = "/exchangeField", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject doExchangeField(@RequestParam String frId,
                                      @RequestParam String srcFID, @RequestParam String desFID,
                                      @RequestParam Integer AID) {
        try {
            //DONE:交换场地
            VO result = erpService.doExchangeField(frId, srcFID, desFID, AID);
            return AjaxObject.ok().data(result.toHashMap(true));

        } catch (Exception e) {
            logger.error("doExchangeField Exception：", e);
            return AjaxObject.error("doExchangeField Exception：" + e.getMessage());
        }
    }

    @RequestMapping(value = "/doUpdateMemberMoney", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject doUpdateMemberMoney(@RequestBody VenueMemberSyncData memberSyncData) {
        try {
            syncService.doUpdateMemberMoney(memberSyncData.vid, VOList.getVOList(memberSyncData.dataList));
            return AjaxObject.ok();
        } catch (Exception e) {
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/doAddMemberInfo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject doAddMemberInfo(@RequestBody VenueMemberSyncData memberSyncData) {
        try {
            syncService.doAddMemberInfo(memberSyncData.vid, VOList.getVOList(memberSyncData.dataList));
            return AjaxObject.ok();
        } catch (Exception e) {
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/doAddMemberRechargeInfo", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject doAddMemberRechargeInfo(@RequestBody VenueMemberSyncData memberSyncData) {
        try {
            syncService.doAddMemberRechargeInfo(memberSyncData.vid, VOList.getVOList(memberSyncData.dataList));
            return AjaxObject.ok();
        } catch (Exception e) {
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/getMemberCardPrice", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getMemberCardPrice(@RequestParam int vid,
                                         @RequestParam int memberCardId,
                                         @RequestParam String fpIds) {
        try {
            VO retVO = fieldReserveRule.getMemberCardPrice(vid, memberCardId, fpIds);
            return AjaxObject.ok().put("data", retVO.toHashMap(true));
        } catch (Exception e) {
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/getMemberCardPriceForImmediatelyOrder", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject getMemberCardPriceForImmediatelyOrder(@RequestParam int vid,
                                                            @RequestParam int memberCardId, @RequestParam String fIds,
                                                            @RequestParam String beginTime, @RequestParam double useTime) {
        try {
            VO retVO = fieldReserveRule.getMemberCardPriceForImmediatelyOrder(vid, memberCardId, fIds, beginTime, useTime);
            return AjaxObject.ok().put("data", retVO.toHashMap(true));
        } catch (Exception e) {
            return AjaxObject.error(e.getMessage());
        }
    }

    @RequestMapping(value = "/checkVenueOrderRule", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public AjaxObject checkVenueOrderRule(@RequestParam int vid,
                                          @RequestParam String fpIds) {
        try {
            fieldReserveRule.doCheckVenueOrderRule(vid, fpIds);
            return AjaxObject.ok();
        } catch (Exception e) {
            return AjaxObject.error(e.getMessage());
        }
    }


}
